package com.xiexin.web.controller;

import com.xiexin.common.core.domain.AjaxResult;
import com.xiexin.member.service.IMemberService;
import com.xiexin.setmealinfo.service.ISetmealInfoService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 报表操作
 */
@RestController
@RequestMapping("/api/report")
public class ReportController {
    @Resource
    private IMemberService memberService;
    @Resource
    private ISetmealInfoService setmealInfoService;


    //会员数量折线图数据
    @RequestMapping("/getMemberReport")  // "/api/report/getMemberReport
    public AjaxResult getMemberReport(){
        Map<String,Object> map = new HashMap<>();
        List<String> months = new ArrayList();
        Calendar calendar = Calendar.getInstance();//获得日历对象，模拟时间就是当前时间
        //计算过去一年的12个月
        calendar.add(Calendar.MONTH,-12);//获得当前时间往前推12个月的时间
        for(int i=0;i<12;i++){
            calendar.add(Calendar.MONTH,1);//获得当前时间往后推一个月日期
            Date date = calendar.getTime();
            months.add(new SimpleDateFormat("yyyy-MM").format(date));
        }
        map.put("months",months);

      List<Integer> memberCount = memberService.findMemberCountByMonths(months);
           map.put("memberCount",memberCount);

        return   AjaxResult.success(map);
    }


    //套餐预约占比饼形图
    @RequestMapping("/getSetmealReport")    //  /api/report/getSetmealReport
    public AjaxResult getSetmealReport(){
        //使用模拟数据测试使用什么样的java对象转为饼形图所需的json数据格式
        Map<String,Object> data = new HashMap<>();
        try{
            List<Map<String,Object>> setmealCount = setmealInfoService.findSetmealCount();
            data.put("setmealCount",setmealCount);

            List<String> setmealNames = new ArrayList<>();
            for (Map<String, Object> map : setmealCount) {
                String name = (String) map.get("name");//套餐名称
                setmealNames.add(name);
            }

            data.put("setmealNames",setmealNames);
            return  AjaxResult.success(data);
        }catch (Exception e){
            e.printStackTrace();
            return  AjaxResult.success( "获取套餐统计数据失败");
        }
    }



}
